﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.BaseDoc;
using System.IO;
using SmartWeb.BLL;

public partial class Task_BaseDoc_AddBaseDoc : System.Web.UI.Page
{
    BaseDocDataContext db = new BaseDocDataContext();
    private List<string> listName = new List<string>();
    private List<string> listEx = new List<string>();
    private List<string> listTitle = new List<string>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            init();
        }
    }

    private void init()
    {
        BindType();
        BindDateList();
        if (Request.QueryString["ID"] == null)
        {
            Caption.Text = "新增档案";
        }
        else
        {
            Caption.Text = "修改档案";
        }
        ddlType_SelectedIndexChanged(this, new EventArgs());

    }

    private void BindDateList()
    {
        for (int i = 2008; i <= 2050; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "年";
            li.Value = i.ToString();
            DptYear.Items.Add(li);
        }
        for (int i = 1; i <= 12; i++)
        {
            this.DptMonth.Items.Add(new ListItem(i.ToString() + "月", i.ToString()));
        }
        DptMonth.SelectedValue = DateTime.Now.Month.ToString();
        DptYear.SelectedValue = DateTime.Now.Year.ToString();
    }

    private void BindType()
    {
        ddlType.DataSource = db.BaseDocType.Where<BaseDocType>(o => o.ParentID == 0);
        ddlType.DataBind();
        BindClass();
    }

    private void BindClass()
    {

        ddlClass.DataSource = db.BaseDocType.Where<BaseDocType>(o => o.ParentID == Convert.ToInt32(ddlType.SelectedValue));
        ddlClass.DataBind();
        if (ddlClass.Items.Count == 0)
        {
            ddlClass.Visible = false;
        }
        else
        {
            ddlClass.Visible = true;
        }
    }

    private int GetDID()
    {
        if (ddlClass.Visible)
            return int.Parse(ddlClass.SelectedValue);
        else
            return int.Parse(ddlType.SelectedValue);
    }

    //多文件上传
    private Boolean SaveImages()
    {
        ///'遍历File表单元素
        HttpFileCollection files = HttpContext.Current.Request.Files;
        if (files.Count < 1)
        {
            if (ddlType.SelectedValue.Equals("2"))
            {
                lblPrompt.Text = "请您选择要上传的附件";
                return false;
            }
        }
        /// '状态信息
        System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
        strMsg.Append("上传的文件分别是：<hr color=red>");
        //try
        //{
        for (int iFile = 0; iFile < files.Count; iFile++)
        {
            ///'检查文件扩展名字
            HttpPostedFile postedFile = files[iFile];
            string fileName, fileExtension, fileTitle;
            fileTitle = System.IO.Path.GetFileName(postedFile.FileName);
            if (fileTitle != "")
            {
                fileExtension = System.IO.Path.GetExtension(fileTitle);
                string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
                string mapPath = "~/Document/" + DepartmentID + "/";
                fileName = Server.MapPath(mapPath);
                if (!Directory.Exists(fileName))
                {
                    Directory.CreateDirectory(fileName);
                }
                //fileName += fileTitle;
                ///'可根据扩展名字的不同保存到不同的文件夹
                ///注意：可能要修改你的文件夹的匿名写入权限。
                string temp = DateTime.Now.ToString("yyMMddhhmmss") + iFile.ToString("000") + fileTitle;
                fileName += temp;
                postedFile.SaveAs(fileName);
                listEx.Add(fileExtension);
                listName.Add(temp);
                listTitle.Add(fileTitle);

            }
            else
            {
                if (ddlType.SelectedValue.Equals("2"))
                {
                    lblPrompt.Text = "请您选择要上传的附件";
                    return false;
                }
            }

        }

        return true;
        //}
        //catch (System.Exception Ex)
        //{
        //    //strStatus.Text = Ex.Message;
        //    return false;
        //}
    }
    #region Web 窗体设计器生成的代码

    protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindClass();

        if (ddlType.SelectedValue.Equals("3"))
        {
            txtAddress.Visible = true;
            lbladd.Visible = true;
            pn.Visible = false;
        }
        else
        {
            pn.Visible = true;
        }
        if (ddlType.SelectedValue.Equals("2"))
        {
            txtAddress.Visible = false;
            lbladd.Visible = false;
            pn.Visible = true;

        }
        if (ddlType.SelectedValue.Equals("1"))
        {
            txtAddress.Visible = false;
            lbladd.Visible = false;
        }
        
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        lblPrompt.Text = "";
        if (!SaveImages())
            return;

        Yipusoft.Account.Model.UserInfo user = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        DateTime dt = new DateTime(int.Parse(DptYear.SelectedValue), int.Parse(DptMonth.SelectedValue), 1);
        string dID = Doc.InsertDoc(txtCaption.Text,txtAddress.Text, user.UserID, user.DepartmentID, dt, "", GetDID());
        if (string.IsNullOrEmpty(dID))
            return;

        for (int i = 0; i < listTitle.Count(); i++)
        {
            Doc.InsertDocFile(dID, listEx[i].ToString(), listName[i].ToString() , listTitle[i].ToString());
        }

        lblPrompt.Text = "操作已成功";
        ClearData();
    }

    public string GetButton()
    {
        if (ddlType.SelectedValue.Equals("1"))
            return "<input type='button' value='增加(Add)' onclick='addFile()'>";
        else
            return "";
    }

    private void ClearData()
    {
        txtCaption.Text = "";
        txtAddress.Text = "Http://";

    }
    #endregion
}
